package com.zjxh.zjxh.mob.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.zjxh.zjxh.common.Result;
import com.zjxh.zjxh.common.constant.CMEnum;
import com.zjxh.zjxh.common.constant.Constant;
import com.zjxh.zjxh.common.exception.CommonException;
import com.zjxh.zjxh.common.req.IdReq;
import com.zjxh.zjxh.common.req.PageKeywordReq;
import com.zjxh.zjxh.common.util.NPage;
import com.zjxh.zjxh.common.util.PageUtil;
import com.zjxh.zjxh.mob.mapper.FileCommonMapper;
import com.zjxh.zjxh.mob.mapper.MAppealImageMapper;
import com.zjxh.zjxh.mob.mapper.MobAppealMapper;
import com.zjxh.zjxh.mob.mapper.MobEvaluateMapper;
import com.zjxh.zjxh.mob.mapper.TExpertMapper;
import com.zjxh.zjxh.mob.service.ApplicationClaimService;
import com.zjxh.zjxh.mob.vo.req.ClaimInsertReq;
import com.zjxh.zjxh.mob.vo.req.EvaluateReq;
import com.zjxh.zjxh.mob.vo.resp.ExpertInfoResp;
import com.zjxh.zjxh.mob.vo.resp.ExpertListResp;
import com.zjxh.zjxh.pojo.FileCommon;
import com.zjxh.zjxh.pojo.MAppealImage;
import com.zjxh.zjxh.pojo.MobAppeal;
import com.zjxh.zjxh.pojo.MobEvaluate;
import com.zjxh.zjxh.pojo.TExpert;
import java.time.LocalDateTime;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author: oyp
 * @date: 2024/5/28 on 17:07
 * @desc:
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class ApplicationClaimServiceImpl implements ApplicationClaimService {

  private final TExpertMapper tExpertMapper;
  private final MAppealImageMapper mAppealImageMapper;
  private final MobAppealMapper mobAppealMapper;
  private final MobEvaluateMapper mobEvaluateMapper;
  private final FileCommonMapper fileCommonMapper;

  public ApplicationClaimServiceImpl(
      TExpertMapper tExpertMapper,
      MAppealImageMapper mAppealImageMapper,
      MobAppealMapper mobAppealMapper,
      MobEvaluateMapper mobEvaluateMapper,
      FileCommonMapper fileCommonMapper) {
    this.tExpertMapper = tExpertMapper;
    this.mAppealImageMapper = mAppealImageMapper;
    this.mobAppealMapper = mobAppealMapper;
    this.mobEvaluateMapper = mobEvaluateMapper;
    this.fileCommonMapper = fileCommonMapper;
  }

  /**
   * 专家列表
   *
   * @param request
   * @param req
   * @return
   */
  @Override
  public Result<NPage<ExpertListResp>> expertList(HttpServletRequest request, PageKeywordReq req) {
    IPage<TExpert> iPage = new Page<>(req.getPageNum(), req.getPageSize());
    iPage =
        tExpertMapper.selectPage(
            iPage,
            Wrappers.<TExpert>lambdaQuery()
                .select(TExpert::getId, TExpert::getAreaOfExpertise, TExpert::getName)
                .like(StrUtil.isNotBlank(req.getKeyword()), TExpert::getName, req.getKeyword())
                .orderByDesc(TExpert::getCreateAt));
    List<TExpert> records = iPage.getRecords();
    List<ExpertListResp> respList = Lists.newArrayList();
    if (!records.isEmpty()) {
      records.forEach(
          expert ->
              respList.add(
                  new ExpertListResp()
                      .setId(expert.getId())
                      .setName(expert.getName())
                      .setAreaOfExpertise(expert.getAreaOfExpertise())
                      .setImageUrl("")));
    }
    return Result.success(PageUtil.toNPage(iPage, respList));
  }

  /**
   * @param request
   * @param req
   * @return
   */
  @Override
  public Result<Object> insetClaim(HttpServletRequest request, ClaimInsertReq req) {
    MobAppeal mobAppeal = new MobAppeal().setCreateAt(LocalDateTime.now());
    BeanUtil.copyProperties(req, mobAppeal);
    int insert = mobAppealMapper.insert(mobAppeal);
    if (insert != Constant.INT_ONE) {
      throw new CommonException(CMEnum.ERROR_INSERT);
    }
    if (!req.getImageIdList().isEmpty()) {
      req.getImageIdList()
          .forEach(
              id -> {
                int insert1 =
                    mAppealImageMapper.insert(
                        new MAppealImage()
                            .setMAppealId(mobAppeal.getId())
                            .setFileId(Long.valueOf(id))
                            .setCreateAt(LocalDateTime.now()));
                if (insert1 != Constant.INT_ONE) {
                  throw new CommonException(CMEnum.ERROR_INSERT);
                }
              });
    }
    return Result.success(CMEnum.SUCCESS);
  }

  /**
   * 添加评价
   *
   * @param request
   * @param req
   * @return
   */
  @Override
  public Result<Object> insetEvaluate(HttpServletRequest request, EvaluateReq req) {
    int insert =
        mobEvaluateMapper.insert(
            new MobEvaluate()
                .setContext(req.getContext())
                .setSatisfaction(req.getSatisfactionEnum())
                .setCreateAt(LocalDateTime.now()));
    if (insert != Constant.INT_ONE) {
      throw new CommonException(CMEnum.ERROR_INSERT);
    }
    return Result.success(CMEnum.SUCCESS);
  }

  /**
   * 专家详情
   *
   * @param request
   * @param req
   * @return
   */
  @Override
  public Result<ExpertInfoResp> expertInfo(HttpServletRequest request, IdReq req) {
    ExpertInfoResp resp = new ExpertInfoResp();
    TExpert tExpert =
        tExpertMapper.selectOne(Wrappers.<TExpert>lambdaQuery().eq(TExpert::getId, req.getId()));
    BeanUtil.copyProperties(tExpert, resp);
    FileCommon fileCommon =
        fileCommonMapper.selectOne(
            Wrappers.<FileCommon>lambdaQuery()
                .select(FileCommon::getId, FileCommon::getPath)
                .eq(FileCommon::getId, tExpert.getImageId()));
    return Result.success(
        resp.setSex(tExpert.getSex().getDesc()).setImageUrl(fileCommon.getPath()));
  }
}
